<?php
$is_admin = $_SESSION['status_code'] == 'ADMIN';
if(!$is_admin) {
	echo '<div id="contenus_ajouter"><h3>Ajout de contenu</h3><p class="error">Vous n\'avez pas accès à cette section</p></div>';
}
else {

require_once('lib/menu.php');
require_once('lib/content.php');

if(isset($_GET['id'])) {
	$content = content_get_by_id($_GET['id']);
	if(empty($content)) {
		echo '<p class="error">Ce contenu n\'existe pas</p>';
		$edition = 0;
		$locked = '';
	}
	else {
		$edition = 1;
		$locked = $content['locked'] ? 'disabled="disabled"' : '';
	}
}
else {
	$content = array('idMenu' => '-1', 'id' => '', 'title' => '', 'text' => '');
	$edition = 0;
	$locked = '';
}

$action = $edition ? 'content_update' : 'content_add';
$old_menu = $edition ? $content['idMenu'] : '';
$old_id = $edition ? $_GET['id'] : '';

?>
<script src="javascript/tiny_mce/tiny_mce.js" type="text/javascript"></script>
<script src="javascript/tiny_mce/init.js" type="text/javascript"></script>
<script src="pages_admin/redaction/_redaction.js" type="text/javascript"></script>
<div id="contenus_ajouter">
	<h3><?php echo $edition ? 'Modification de contenu' : 'Ajout de contenu'; ?></h3>
	<div class="ajax"></div>
	<form method="post" action="pages_admin/redaction/_redaction_ajax.php">
		<p>
			<input type="hidden" name="action" id="action" value="<?php echo $action; ?>" />
			<input type="hidden" name="old_menu" id="old_menu" value="<?php echo $old_menu; ?>" />
			<input type="hidden" name="old_id" id="old_id" value="<?php echo $old_id; ?>" />
		</p>
		<p>
			<label for="menu">Menu associé</label><br />
			<select name="menu" id="menu" <?php echo $locked; ?>>
				<?php
				echo $edition && $content['idMenu'] != -1 ? '<option value="-1"></option><option value="'.$content['idMenu'].'" selected="selected">'.menu_get_name($content['idMenu']).'</option>' : '<option value="-1"></option>';
				$unused_menus = menu_get_unused();
				while($menu = mysql_fetch_assoc($unused_menus)) {
					echo '<option value="'.$menu['id'].'">'.$menu['name'].'</option>';
				}
				?>
			</select>
		</p>
		<p>
			<label for="string_id">Identifiant unique (<em>affiché dans la barre d'adresse</em>)</label><br />
			<input type="text" name="string_id" id="string_id" size="60" value="<?php echo $content['id']; ?>" <?php echo $locked; ?> />
		</p>
		<p>
			<label for="title">Titre affiché en tête de page</label><br />
			<input type="text" name="title" id="title" size="60" value="<?php echo $content['title']; ?>" />
		</p>
		<p>
			<label for="content">Texte</label><br />
			<textarea name="content" id="content" rows="10" cols="70"><?php echo $content['text']; ?></textarea>
		</p>
		<p>
			<input type="submit" name="<?php echo $action; ?>" id="<?php echo $action; ?>" value="<?php echo $edition ? 'Modifier' : 'Ajouter'; ?>" />
		</p>
	</form>
</div>
<?php
}
